/*
 *  PKL 51 GUNUNG KIDUL
 *  Subseksie DATA ENTRY + CAPI
 */
package pkl51.service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import pkl51.database.DatabaseUtility;
import pkl51.entity.Pengawasan;
import pkl51.error.EntityException;
import pkl51.service.PengawasanDao;

/**
 *
 * @author Hindarwan
 */
public class PengawasanlDaoImpl implements PengawasanDao{

    private Connection connection;
//    private final String insertKorwil = "INSERT INTO pengawasan VALUES (?, ?, ?)";
//    private final String updateKorwil = "UPDATE korwil SET nim_korwil = ?, kode_kec = ? WHERE nim_kortim = ?";
    private final String getPengawasanByNimPencacah = "SELECT * FROM pengawasan WHERE nim_pencacah = ?";
            
    @Override
    public Pengawasan getPengawasanByNim_Pencacah(String nim_pencacah) throws EntityException {
        PreparedStatement statement = null;
        try {
            connection = DatabaseUtility.getConnection();
            connection.setAutoCommit(false);
            statement = connection.prepareStatement(getPengawasanByNimPencacah);

            statement.setString(1, nim_pencacah);
            
            ResultSet resultSet = statement.executeQuery();
            
            Pengawasan pengawasan = null;
            if(resultSet.next()){
                pengawasan = new Pengawasan();
                pengawasan.setNim_pencacah(resultSet.getString("nim_pencacah"));
                pengawasan.setNim_kortim(resultSet.getString("nim_kortim"));
            } else{
                throw new EntityException("nim pencacah tidak ditemukan");
            }
            connection.commit();
            
            return pengawasan;

        } catch (SQLException ex) {
            try {
                connection.rollback();
            } catch (SQLException ex1) {
                Logger.getLogger(PengawasanlDaoImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
            Logger.getLogger(PengawasanlDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new EntityException();
        } finally {
            try {
                connection.setAutoCommit(true);
            } catch (SQLException ex) {
                Logger.getLogger(PengawasanlDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(PengawasanlDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

//    public void insertPengawasan(Pengawasan korwil) throws EntityException {
//        PreparedStatement statement = null;
//        try {
//            connection = DatabaseUtility.getConnection();
//            connection.setAutoCommit(false);
//            statement = connection.prepareStatement(insertKorwil);
//
//            statement.setString(1, korwil.getNim_kortim());
//            statement.setString(2, korwil.getNim_korwil());
//            statement.setString(3, korwil.getKode_kec());
//            
//            statement.executeUpdate();
//            
//            connection.commit();
//
//        } catch (SQLException ex) {
//            try {
//                connection.rollback();
//            } catch (SQLException ex1) {
//                System.out.println(ex1.getMessage());
//            }
//            System.out.println(ex.getMessage());
//        } finally {
//            try {
//                connection.setAutoCommit(true);
//            } catch (SQLException ex) {
//                System.out.println(ex.getMessage());
//            }
//            if (statement != null) {
//                try {
//                    statement.close();
//                } catch (SQLException ex) {
//                    System.out.println(ex.getMessage());
//                }
//            }
//        }
//    }
//
//    public void updatePengawasan(Pengawasan korwil) throws EntityException {
//        PreparedStatement statement = null;
//        try {
//            connection = DatabaseUtility.getConnection();
//            connection.setAutoCommit(false);
//            statement = connection.prepareStatement(updateKorwil);
//
//            statement.setString(1, korwil.getNim_korwil());
//            statement.setString(2, korwil.getKode_kec());
//            statement.setString(3, korwil.getNim_kortim());
//            
//            statement.executeUpdate();
//
//            connection.commit();
//
//        } catch (SQLException ex) {
//            try {
//                connection.rollback();
//            } catch (SQLException ex1) {
//                System.out.println(ex1.getMessage());
//            }
//            System.out.println(ex.getMessage());
//        } finally {
//            try {
//                connection.setAutoCommit(true);
//            } catch (SQLException ex) {
//                System.out.println(ex.getMessage());
//            }
//            if (statement != null) {
//                try {
//                    statement.close();
//                } catch (SQLException ex) {
//                    System.out.println(ex.getMessage());
//                }
//            }
//        }
//    }
//    

    @Override
    public void insertPengawasan(Pengawasan pengawasan) throws EntityException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void updatePengawasan(Pengawasan pengawasan) throws EntityException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
